/*
 * Copyright 2010 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma.internal.client.view.adapter;

import org.shalma.client.ui.MagicButton;
import org.shalma.client.ui.MagicButtonAction;
import org.shalma.client.ui.MagicButtonControl;
import org.shalma.client.ui.MagicButtonListener;
import org.shalma.client.ui.icon.ShalmaResources;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ListUI extends Composite {

	private FlexTable table;

	private ListUIAdapter adapter;

	private EventHandler eventHandler;

	private static ListMagicButtonControl magicButtonControl = new ListMagicButtonControl();

	// ///

	public ListUI(ListUIAdapter adapter) {

		this.adapter = adapter;
		eventHandler = new EventHandler();

		table = new FlexTable();
		table.unsinkEvents(Event.ONCLICK);

		table.setCellPadding(4);
		table.setCellSpacing(2);
		initWidget(table);

		update();
	}

	// ///

	public void update() {

		table.clear();

		if (adapter.size() == 0)
			addEmptyIcon();
		else
			for (int i = 0; i < adapter.size(); i++) {
				addMagicButton(i);
				adapter.display(i, table, i);
			}
	}

	// ///

	void make(int index) {

		if (adapter.size() == 0)
			table.clear();

		table.insertRow(index);
		addMagicButton(index);
		adapter.make(index, table, index);
	}

	void remove(int index) {
		table.removeRow(index);
		adapter.remove(index);
		if (table.getRowCount() == 0)
			addEmptyIcon();
	}

	// ///

	private void addEmptyIcon() {
		Image empty = new Image(ShalmaResources.instance.list());
		empty.addClickListener(eventHandler);
		table.setWidget(0, 0, empty);
	}

	private void addMagicButton(int row) {
		MagicButton button = new MagicButton(eventHandler);
		button.addControl(magicButtonControl);
		table.setWidget(row, 0, button);
		table.getCellFormatter().setVerticalAlignment(row, 0,
				HasAlignment.ALIGN_TOP);
	}

	private int getIndex(Widget widget) {

		int index = -1;

		if (widget == null)
			return index;

		Element tr = DOM.getParent(DOM.getParent(widget.getElement()));
		Element body = DOM.getParent(tr);
		return DOM.getChildIndex(body, tr);
	}

	// ///

	class EventHandler implements ClickListener, MagicButtonListener {

		public void onClick(Widget sender) {
			make(0);
		}

		public void onEvent(MagicButton button, MagicButtonAction action) {

			int index = getIndex(button);

			if (MagicButtonAction.RIGHT == action)
				remove(index);
			else if (MagicButtonAction.UP == action)
				make(index);
			else if (MagicButtonAction.DOWN == action)
				make(index + 1);
		}
	}

	static class ListMagicButtonControl implements MagicButtonControl {

		public boolean enabled(MagicButtonAction action) {
			return action == MagicButtonAction.UP
					|| action == MagicButtonAction.RIGHT
					|| action == MagicButtonAction.DOWN;
		}

		public boolean confirm(MagicButtonAction action) {
			return action == MagicButtonAction.RIGHT;
		}

		public Widget makeDescriptor(MagicButtonAction action) {
			if (action == MagicButtonAction.UP)
				return new Label("Insert before");
			else if (action == MagicButtonAction.RIGHT)
				return new Label("Delete");
			else
				return new Label("Insert after");
		}
	}
}